package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService detailService;


    /**
     * spu的分页查询
     *
     * @param page
     * @param rows
     * @param saleable
     * @param brandId
     * @param categoryId
     * @param id
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id) {

        return ResponseEntity.ok(this.spuService.pageQuery(page, rows, saleable, brandId, categoryId, id));
    }

    /**
     * 根据商品id（spuId）查询对应的规格参数组集合以及组内参数的信息，及对应值
     *
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpec(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        return ResponseEntity.ok(this.spuService.listSpec(spuId, searching));
    }


    /**
     * 商品新增
     *
     * @param spuDTO
     * @return
     */
    @PostMapping(value = "/spu")
    public ResponseEntity<Void> addGoods(
            @RequestBody SpuDTO spuDTO) {

        this.spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据sku id的集合查询对应的sku的集合
     *
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(this.skuService.listSkuByIds(ids));
    }


    /**
     * 根据spuId查询对应的sku集合
     *
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpu(@RequestParam("id") Long spuId) {

        return ResponseEntity.ok(this.skuService.listSkuBySpu(spuId));
    }

    /**
     * 根据spuId查询对应的spuDetail
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long spuId) {

        return ResponseEntity.ok(this.detailService.querySpuDetailById(spuId));
    }

    /**
     * spu的基本基本查询
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spuId) {

        return ResponseEntity.ok(this.spuService.querySpuById(spuId));
    }

    /**
     * 根据spuId查询商品信息
     *
     * @param spuId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoods(@PathVariable("id") Long spuId) {

        return ResponseEntity.ok(this.spuService.queryGoods(spuId));
    }

    /**
     * 修改商品的上下架状态
     *
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {

        this.spuService.modifySaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 商品修改
     *
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(
            @RequestBody SpuDTO spuDTO) {

        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }
}
